<?php
/* 
 * 首页推荐 网站广告 操作类
 * 程序编写：pennyowen/pepsi.zh
 * 联系邮箱：penyowen#gmail.com/pepsi.zh#gmail.com
 * 修改时间：2008-9-30
 */
if (!defined('IN_SITE')){ die('Hacking attempt!'); }
interface AdvDbInter{
	### 功能：提交一个广告记录  参数：$post 提交的内容 #####
    function submitRecord($post);
	### 功能：通过一个记录id获得一条广告记录  参数：$id 记录id #####
    function getOneRecord($id);
    ### 功能：获得某个类型的广告  参数：$typeid 广告类型id；$limit 返回的记录条数 #####
    function getRecordOfOneType($typeid,$limit=10,$tlong=0,$clong=0,$order='Atop desc');
    ### 功能：删除广告记录  参数：$id 广告id，可以是数组也可以是单id #####
    function deleteRecord($id);
    ####--------------------广告类型管理------------------------------####
    ### 功能：提交一个广告类型记录  参数：$post 提交的内容 #####
    function submitTypeRecord($post);
    ### 功能：删除广告类型记录  参数：$id 广告类型id #####
    function deleteTypeRecord($id);
    ### 功能：得到广告类别select列表  参数：$type 广告类别的类型 0首页推荐类型 1为广告类型 #####
    function getTypeList($type=0);
    ### 功能：得到广告类别列表  参数：$type 广告类别的类型 0首页推荐类型 1为广告类型 #####
    function getTypeRecord($type=0);
}
class adv implements AdvDbInter
{
	public  $error = array();
	public  $action = '提交';
	function submitRecord($post){
		global $dbconn,$dbtable;
		$post = acrossTrim($post);
		$flag = true;
		if (strlen($post[Asubject]) < 1){
			$flag = false;
			$this->error[e_Asubject] = '广告标题不能为空';
		}
		if (empty($post[CAid])){
			$flag = false;
			$this->error[e_CAid] = '广告类别不能为空';
		}
		$urllen = strlen($post[Asiteurl]);
		if ($urllen < 1){
			$flag = false;
			$this->error[e_Asiteurl] = '广告地址不能为空';
		}elseif ($urllen > 255){
			$flag = false;
			$this->error[e_Asiteurl] = '广告地址不能超过255字节';
		}
		if ($post[unsetValue]){
			foreach ($post[unsetValue] as $value){
				$post[$value] = '';
			}
		}
		unset($post[unsetValue]);
		if ($flag){
			if(is_uploaded_file($_FILES['imagefile']['tmp_name'])){
				srand(time(0)); 
				$random =rand(1,100); 
				$upload_file_name = date("YmdHis",time()).$random;
				$filetype = getPicType($_FILES['imagefile']['type']);
				$save_path = UPLOAD_ROOT.$upload_file_name.$filetype;
				if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$save_path)){
					$post[Aadimage] = $upload_file_name.$filetype;
				}
			}
			if ($post[Adid]){
				$this->action = '修改';
				return $dbconn->Query_update($post,$dbtable->adv,array('Adid'=>$post[Adid]));
			}else {
				$this->action = '添加';
				return $dbconn->Query_adate($post,$dbtable->adv);
			}
		}else {
			return false;
		}
	}
	function getOneRecord($id){
		global $dbconn,$dbtable;
		if ($id){ $id = intval($id); }else{ return false ;}
		$query = "select * from ".$dbtable->adv." where Ashow='y' and Adid=$id limit 1";
		$record = $dbconn->Getonerecord($query);
		if ($record[Aadimage] && file_exists(UPLOAD_ROOT.$record[Aadimage])){
			$record[Aadimage_show] = UPLOAD_URL.$record[Aadimage];
		}else {
			$record[Aadimage] = '';
		}
		return $record;
	}
	function getRecordOfOneType($typeid,$limit=10,$tlong=0,$clong=0,$order='Atop desc'){
		global $dbconn,$dbtable;
		if ($typeid){ $typeid = intval($typeid); }else { return false ;}
		
		if ($limit){ $limit = "limit 0,".intval($limit);}
		else { $limit = ""; }
		
		if ($order){ $order = "order by $order";}
		else { $order = '';}

		$query = "select * from ".$dbtable->adv." where Ashow='y' and CAid=$typeid $order $limit";
		$nowQueryId = $dbconn->Query($query);
		$num = 1;
		while (($row=$dbconn->next_record($nowQueryId))){
			$row[num] = $num;
			if ($tlong){
				$row[Asubject_cut] = cut_str($row[Asubject],$tlong);
			}else {
				$row[Asubject_cut] = $row[Asubject];
			}
			if ($clong){
				$row[Acontent_cut] = cut_str($row[Acontent],$clong);
			}else {
				$row[Acontent_cut] = $row[Acontent];
			}
			if ($row[Aadimage] && file_exists(UPLOAD_ROOT.$row[Aadimage])){
				$row[Aadimage] = UPLOAD_URL.$row[Aadimage];
			}else {
				$row[Aadimage] = '';
			}
			$num++;
			$record[] = $row;
		}
		return $record;
	}
	function deleteRecord($id){
		global $dbconn,$dbtable;
		if (empty($id)){ return false ;}
		if (is_array($id)){
			$where  = "Adid in (".implode(',',$id).")";
		}else {
			$where  = "Adid=$id limit 1";
		}
		return $dbconn->Query_update(array('Ashow'=>'n'),$dbtable->adv,"where ".$where);
	}
    ####--------------------广告类型管理------------------------------####
    function submitTypeRecord($post){
    	global $dbconn,$dbtable;
    	$post = acrossTrim($post);
    	$flag = true;
    	if (strlen($post[CAcataname]) < 1){
    		$flag = false;
			$this->error[e_CAcataname] = '广告类别名称不能为空';
    	}
    	if ($flag){
    		if ($post[CAid]){
    			$this->action = '修改';
    			$query = "select CAid from ".$dbtable->advcata." where CAcataname='".$post[CAcataname]."' and CAid!=".$post[CAid];
    			if ($dbconn->Getonerecord($query)){
	    			$flag = false;
					$this->error[e_CAcataname] = '广告类别名称已经存在了，请换一个';
    			}else {
    				return $dbconn->Query_update($post,$dbtable->advcata,array('CAid'=>$post[CAid]));
    			}
    		}else {
    			$this->action = '添加';
    			$query = "select CAid from ".$dbtable->advcata." where CAcataname='".$post[CAcataname]."'";
    			if ($dbconn->Getonerecord($query)){
	    			$flag = false;
					$this->error[e_CAcataname] = '广告类别名称已经存在了，请换一个';
    			}else {
    				return $dbconn->Query_adate($post,$dbtable->advcata);
    			}
    		}
    	}else{
    		return false;
    	}
    	return false;
    }
    function deleteTypeRecord($id){
    	global $dbtable;
    	if (empty($id)) { return false ;}
    	else { $id = intval($id); }
    	return deleteRecord($dbtable->advcata," CAid=$id");
    }
    function getTypeList($type=0){
    	global $dbconn,$dbtable;
    	$query = "select CAid,CAcataname from ".$dbtable->advcata." where CAtype=$type order by CAid asc";
    	return $dbconn->Fetcharray_area($query,'CAid','CAcataname');
    }
    function getTypeRecord($type=0){
    	global $dbconn,$dbtable;
    	$query = "select * from ".$dbtable->advcata." where CAtype=$type order by CAid asc";
    	return $dbconn->Fetcharraynum($query);
    }
    function getOneType($id){
    	global $dbconn,$dbtable;
    	$query = "select * from ".$dbtable->advcata." where CAid=$id limit 1";
    	return $dbconn->Getonerecord($query);
    }
}
?>